package com.cf.service.impl;

import com.cf.dao.IdCardDao;
import com.cf.dao.UserDao;
import com.cf.entity.IdCardConfirm;
import com.cf.entity.User;
import com.cf.service.IdCardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
@Service
@Transactional
public class IdCardServiceImpl implements IdCardService {
    @Autowired
    private IdCardDao idCardDao;
    @Autowired
    private UserDao userDao;
    @Override
    public int addCard(IdCardConfirm idCardConfirm) {
        return idCardDao.addCard(idCardConfirm);
    }

    @Override
    public PageInfo<IdCardConfirm> selectCard(int pagenum, int pagesize, Map map) {
        PageHelper.startPage(pagenum,pagesize);
        List<IdCardConfirm> idCardConfirms = idCardDao.selectCard(map);
        for(IdCardConfirm idcard:idCardConfirms){
            String s = idCardDao.selectUsername(idcard.getId());
            idcard.setUsername(s);
        }
        PageInfo<IdCardConfirm> pageInfo =new PageInfo<>(idCardConfirms);
        return pageInfo;
    }


    @Override
    public int updateCard(long id) {
        int i = idCardDao.updateCard(id);
        if (i!=0){
            IdCardConfirm idCardConfirm = idCardDao.selectByIdCardId(id);
            int i1 = userDao.updateIdCardStatus(idCardConfirm.getUserid());
            if (i1!=0){
                return i1;
            }else {
                return i1;
            }
        }else {
            return i;
        }
    }

    @Override
    public int delCard(long id) {
        return idCardDao.delCard(id);
    }

    @Override
    public int updateReason(IdCardConfirm idCardConfirm) {
        return idCardDao.updateReason(idCardConfirm);
    }

    @Override
    public IdCardConfirm selectIdCardById(long id) {
        IdCardConfirm idCardConfirm = idCardDao.selectIdCardById(id);
        if(idCardConfirm!=null){
            User user = userDao.selectUserById(id);
            idCardConfirm.setUsername(user.getUsername());
        }
        return idCardConfirm;
    }
    @Override
    public IdCardConfirm selectIdById(long id) {
        return idCardDao.selectIdById(id);
    }

}
